<template>
    <div style="max-width: 960px;margin: 0 auto;">
        <Header></Header>
        <el-container>
            <el-header>
                <div style="margin-left: 20px;margin-top: 20px;font-size: large;display: flex;align-items: center;"><i
                        class="el-icon-mobile"></i> 手机激活码
                </div>
            </el-header>
            <el-main>
                <el-form style="border-radius: 4px" :model="ruleForm" :rules="rules" ref="ruleForm"
                         class="demo-ruleForm">

                    <el-row>
                        <el-col :span="7">
                            <div class="grid-content bg-purple">
                                <span style="margin-left: 200px;">手机号：</span>
                            </div>
                        </el-col>
                        <el-col :span="9">
                            <div class="grid-content bg-purple-light">
                                <el-form-item style="" prop="mobile">
                                    <el-input type="text" placeholder="请输入手机号" v-model="ruleForm.mobile"
                                              style="align-items: right;display: inline-block"/>
                                </el-form-item>
                            </div>
                        </el-col>
                        <el-col :span="1">
                            <div class="grid-content bg-purple">
                                <span style="color:red;">*</span>
                            </div>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="7">
                            <div class="grid-content bg-purple">
                                <span style="margin-left: 200px;">验证码：</span>
                            </div>
                        </el-col>
                        <el-col :span="6">
                            <div class="grid-content bg-purple-light">
                                <el-form-item style="" prop="authCode">
                                    <el-input type="text" placeholder="请输入验证码" v-model="ruleForm.authCode"
                                              style="align-items: right;display: inline-block"/>
                                </el-form-item>
                            </div>
                        </el-col>
                        <el-col :span="0.5">
                            <div class="grid-content bg-purple">
                                <span style="color: aliceblue">*</span>
                            </div>
                        </el-col>
                        <el-col :span="8" style="text-align: left;">
                            <div class="grid-content bg-purple">
                                <span v-show="!show" class="count"><span
                                        style="color: red">{{count}}</span> 秒后可以重新发送</span>
                                <button type="button" v-show="show" v-on:click="getCode">发送验证码</button>
                            </div>
                        </el-col>
                    </el-row>
                    <div>
                        <!-- `checked` 为 true 或 false -->
                        <el-checkbox v-model="checked" label=" "/>
                        <div style="display: inline-block;">
                            我已阅读并同意《<a href="/" style=" display: inline-block;color: #409EFF;">企业标准信息公共服务平台服务条款</a>》
                        </div>
                    </div>
                    <el-form-item>
                        <el-button style="margin-top: 20px;width: 100px;margin-right: 100px" type="primary"
                                   @click="onSubmit('loginForm')">确定
                        </el-button>
                    </el-form-item>
                </el-form>

            </el-main>
        </el-container>
    </div>
</template>

<script>
    import Header from "../../../components/Header";
    import {register, setEmailCodeBefore, SMSValid} from '../../../api/userMG'

    export default {
        name: "mobile",
        components: {Header},
        data() {

            return {
                show: true,
                count: '',
                timer: null,
                checked: false,

                ruleForm: {
                    mobile: '',
                    authCode: ''
                },
                rules: {
                    mobile: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                        {min: 11, max: 11, message: '手机号格式不正确', trigger: 'blur'}
                    ],
                    authCode: [
                        {required: true, message: '请输入验证码', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {

            onSubmit() {
                const _this = this;

                if (this.checked === false) {
                    this.$message.error("请仔细阅读企业标准信息公共服务平台服务条款");
                    return false;
                }
                console.log("===========>=====");
                let params2 ={
                    orgId: this.$route.params.data.orgBase.orgId,
                    password: this.$route.params.data.userPo.password,
                }
                console.log(params2);
                const params = {
                    orgId: this.$route.params.data.orgBase.orgId,
                    username: this.$route.params.data.userPo.username,
                    password: this.$route.params.data.userPo.password,
                    mobile: this.$route.params.data.userPo.mobile,
                    fax: this.$route.params.data.userPo.fax,
                    dict: this.$route.params.data.userPo.dict,
                    email: this.$route.params.data.userPo.email,
                    contactName: this.$route.params.data.userPo.contactName,
                    contIdCard: this.$route.params.data.userPo.contIdCard,
                    checkCode: _this.ruleForm.authCode
                };
                console.log("==============");
                console.log(_this.$route.params.data);
                console.log("params", params);
                console.log(_this.ruleForm.authCode);
                console.log("==============");

                SMSValid(_this.ruleForm.authCode).then(res => {
                    console.log("res", res);
                    if (res.code === 200) {
                        register(params).then(
                            res => {
                                if (res.code === 200) {
                                    console.log("params.username", params.username);
                                    _this.$router.push({name: "Success", params: {username: params.username}})
                                }
                            }
                        )
                    }
                    return false;
                });


            },
            getCode() {
                const TIME_COUNT = 60;
                if (!this.timer) {
                    this.count = TIME_COUNT;
                    this.show = false;
                    this.timer = setInterval(() => {
                        if (this.count > 0 && this.count <= TIME_COUNT) {
                            this.count--;
                        } else {
                            this.show = true;
                            clearInterval(this.timer);
                            this.timer = null;
                        }
                    }, 1000)
                }
                setEmailCodeBefore();// 生成验证码
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const _this = this
                        this.$axios.post('/auth', this.ruleForm).then(res => {

                            console.log(res.data)
                            const jwt = res.headers['authorization']
                            const userInfo = res.data.data

                            // 把数据共享出去
                            _this.$store.commit("SET_TOKEN", jwt)
                            _this.$store.commit("SET_USERINFO", userInfo)

                            // 获取
                            console.log(_this.$store.getters.getUser)

                            _this.$router.push("/blogs")
                        })

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

    .el-main {
        background-color: #FFFFFF;
        line-height: 45px;
        text-align: center;
        font-size: 14px;
    }

    .el-icon-mobile:before {
        font-size: 40px;
    }

</style>